// pages/pingtuan/pingtuan.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    member: [{
      name: '苒芮'
    }, {
      name: '苒芮'
    }],
    cha: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 实例化查询对象
    let tableName = 'pingtuan'
    let query = new wx.BaaS.Query()

    // 设置查询条件（比较、字符串包含、组合等）
    //...
    query.compare('isOrder', '=', 0)
    // 应用查询对象
    let Product = new wx.BaaS.TableObject(tableName)
    Product.setQuery(query).find().then(res => {
      // success
      console.log(res.data.objects)
      console.log(app.globalData.product)
      // var items = []
      var items = this.quickSort(res.data.objects)
      console.log(items)
      var kkk = []
      for (var i = 0; i < items.length; i++) {


        for (var j = 0; j < app.globalData.product.length; j++) {
          if (items[i].product_code == app.globalData.product[j].product_code) {
            if (items[i].name != undefined && items[i].phone != undefined) {
              var item = {
                name: items[i].name[0],
                pic: items[i].img[0],
                img: app.globalData.product[j].product_swiper_pic1,
                ren: items[i].ren,
                cha: items[i].ren - items[i].phone.length,
                id: items[i]._id,
                cost: app.cost(wx.getStorageSync('dengji'), app.globalData.product[j].pre_cost),
                zan: false,
                product_name: app.globalData.product[j].product_name,
                ss: items[i].phone.length / items[i].ren * 100
              }
              kkk.push(item)
            }
            if (wx.getStorageSync('zan') != '') {
              for (var l = 0; l < wx.getStorageSync('zan').length; l++) {
                if (wx.getStorageSync('zan')[l] == items[i]._id) {
                  kkk[kkk.length - 1].zan = true

                }
              }
            }
          }

        }
      }

      this.setData({
        member: kkk
      })
    }, err => {
      // err
    })

    // 不设置查询条件
    Product.find().then(res => {
      // success
    }, err => {
      // err
    })

  },
  tiao: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/pingtuan/pingtuan-detail/pingtuan-detail?name=' + this.data.member[e.currentTarget.dataset.postid].id,
    })
  },
  zan: function (e) {
    console.log(e)
    var a = this.data.member
    a[e.currentTarget.dataset.postid].zan = !a[e.currentTarget.dataset.postid].zan
    this.setData({
      member: a
    })
    if (a[e.currentTarget.dataset.postid].zan == true) {
      if (wx.getStorageSync('zan') == '') {
        var kk = []
      } else {
        var kk = wx.getStorageSync('zan')
      }
      kk.push(this.data.member[e.currentTarget.dataset.postid].id)
      wx.setStorageSync('zan', kk)
    }

  },
  quickSort: function (arr) {

    if (arr.length <= 1) {
      return arr;
    }

    var pivotIndex = Math.floor(arr.length / 2);
    console.log(arr)
    var pivot = arr.splice(pivotIndex, 1)[0];
    console.log(pivot.phone.length)
    var left = [];

    var right = [];

    for (var i = 0; i < arr.length; i++) {

      if (arr[i] < pivot.phone.length || arr[i].ren == 2) {

        left.push(arr[i]);

      } else {

        right.push(arr[i]);

      }

    }

    return this.quickSort(left).concat([pivot], this.quickSort(right));

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})